package com.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.AOP.HasRole;
import com.project.common.R;
import com.project.dto.*;
import com.project.dtoplus.AnnexPlus;
import com.project.dtoplus.ProPartPlus;
import com.project.dtoplus.ProjectDetailPlus;
import com.project.entity.Accessory;
import com.project.entity.LogCount;
import com.project.entity.Logs;
import com.project.service.LogCountService;
import com.project.service.LogsService;
import com.project.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private LogsService logsService;

    //1.项目申报
    @PostMapping
    @Transactional
    public R<String> applyNotice(@RequestBody ProjectDto projectDto)  {

        projectService.apply(projectDto);
        return R.success("申报成功");

    }
    //项目申报时上传附件
    @PostMapping("/upload/{type}")
    public R<List<AnnexPlus>> uploadBegin( @RequestPart MultipartFile[] files, @PathVariable("type")  int type) throws IOException {
        List<AnnexPlus> accessories = projectService.addBeginAccessory(files, type);
        return R.success(accessories);
    }
    //2.上传中期，结项等附件
    @PostMapping("/upload/{type}/{projectId}")
    @Transactional
    public  R<String> upload(@RequestBody List<AnnexPlus> annexPluses, @PathVariable("type")  int type,   @PathVariable("projectId") Long projectId) throws IOException {
        projectService.addAccessory(annexPluses,projectId, type);
        return R.success("添加成功");
    }

    //3.项目审核
    @Transactional
    @PostMapping("/audit/{status}")
    public R<String> auditProject(@RequestBody Logs logs,@PathVariable int status){
        projectService.auditPro(logs,status);
        return R.success("审核完成");

    }

    //4.获取项目详情
    @GetMapping("/proDetail")
    public R<ProjectDetailPlus> getDetail(@RequestParam  Long projectId){
        ProjectDetailPlus detail = projectService.detail(projectId);
        return R.success(detail);
    }

    //5.立项审核列表
    @GetMapping("/establishment")
    public R<Page> beginProject(int page,int pageSize,@RequestParam String name){
        Page establishment = projectService.establishment(page, pageSize,name);
        return R.success(establishment);
    }

    //6.未通过详情
    @GetMapping("/errorDetail/{status}")
    public R<ProjectInfo> errorDetail(@RequestParam Long projectId, @PathVariable int status){
        ProjectInfo projectInfo = projectService.msgInfo(status, projectId);
        return R.success(projectInfo);
    }

    //7.项目日志审核
    @PostMapping("/auditLogs")
    public R<String> auditLogs(@RequestBody LogCount logCount){
        projectService.auditLogs(logCount);
     return    R.success("日志审核完成");
    }

    //8.项目进展
    @GetMapping("/logCount")
    public R<Page> logCount(int page,int pageSize,@RequestParam String name){
        Page pageInfo = projectService.logCount(page, pageSize,name);
        return R.success(pageInfo);
    }
    //9.中期审核列表
    @GetMapping("/mid_term")
    public R<Page> pageMid(int page,int pageSize,@RequestParam String name){
        Page pageMid = projectService.pageMid(page, pageSize,name);
        return R.success(pageMid);

    }

    //10.更改成员
    @PreAuthorize("hasAuthority('2')")
    @PutMapping("/modify_members")
    @Transactional
    public R<String> changeMidMember(@RequestBody ChangeMember changeMember){
        Long projectId=changeMember.getProjectId();
        String[] works = changeMember.getWorks();
        String[] userIds = changeMember.getUserIds();
        projectService.changeMember(projectId,userIds,works);
        return R.success("修改成功");
    }

    //11.结项审核列表
    @GetMapping("/closing_item")
    public R<Page> pageFinish(int page,int pageSize,@RequestParam String name){
        Page finish = projectService.pageFinish(page, pageSize,name);
        return R.success(finish);
    }
    //12.提交延期申请
    @PostMapping("/extension/{type}/{projectId}")
    public R<String> extensionAdd(@RequestParam String msg,@PathVariable Long projectId,@PathVariable int type) throws IOException {
        projectService.addExtension(msg,projectId,type);
        return R.success("延期申请提交成功");
    }
    //13.进入材料提交页面
    @GetMapping("/toClose")
    public R<ProPartPlus> toClose(@RequestParam Long projectId){
        ProPartPlus proPartPlus = projectService.toClose(projectId);
        return R.success(proPartPlus);
    }
    //14.提交结项材料
    @PostMapping("/submittals_close")
    @Transactional
    public R<String> finishAccessory(@RequestBody ProjectFinish projectFinish){
        projectService.finishAnnex(projectFinish);
        return R.success("提交成功");
    }


    //老师查询列表
    //1.立项审核列表
    @GetMapping("/teacher/begin")
    public R<Page> teacherBegin(int page,int pageSize,String name){
        Page beginPage = projectService.teacherBeginPage(page, pageSize,name);
        return R.success(beginPage);

    }
    //中期审核列表
    @GetMapping("/teacher/mid")
    public R<Page> teacherMid(int page,int pageSize,String name){
        Page midPage = projectService.teacherMidPage(page, pageSize,name);
        return R.success(midPage);
    }

    //结项审核列表
    @GetMapping("/teacher/end")
    public R<Page> teacherEnd(int page,int pageSize,String name){
        Page endPage = projectService.teacherEndPage(page, pageSize,name);
        return R.success(endPage);
    }

    //延期审核列表
    @GetMapping("/teacher/extension")
    public R<Page> teacherExtension(int page,int pageSize,String name){
        Page extension = projectService.teacherExtension(page, pageSize,name);
        return R.success(extension);

    }
    //更改项目状态
    @HasRole(2)
    @PutMapping("/changeStatus")
    @Transactional
    public R<String> changeProject(@RequestParam Long projectId ,@RequestParam int status){
        projectService.change(projectId,status);
        return R.success("修改状态成功");
    }

    //修改日志次数
    @HasRole(2)
    @PutMapping("/changeLogCount")
    public R<String> changeLogCount(@RequestParam Long projectId ,@RequestParam int count){
        projectService.changeLogCount(projectId,count);
        return R.success("修改成功");
    }
}

